<?php
namespace Admin\Controller;
use Think\Controller;
/**
 * 相册图片管理
 */
class PictureController extends AdminBasicController {
    public $picture = '';
    public function _initialize(){
        $this->checkLogin();
        $this->picture = D('Picture');
    }

    /**
     * 图片列表
     * @author mss
     * @time 2017-12-14
     */
    public function pictureList(){
        $w['status'] = array('NEQ',9);
        if(!empty(I('request.start_time'))&&!empty(I('request.end_time'))){
            $start_time = I('request.start_time');
            $this->assign("start_time",$start_time);
            $end_time = I('request.end_time');
            $this->assign("end_time",$end_time);
            $w['ctime'] = array(array('EGT',strtotime($start_time)),array('ELT',strtotime($end_time)),'and');
            $parameter['start_time'] = I('request.start_time');
            $parameter['end_time'] = I('request.end_time');
        }

        /**按商家名称或账号搜索*/
        if(!empty(trim(I('request.name')))){
            $name = trim(I('request.name'));
            $w['name|account'] = array('LIKE','%'.$name.'%');
            $w['status'] = array('NEQ',9);
            $ids = M('Shop')->where($w)->order('ctime DESC')->getField('shop_id',true);
            $where['shop_id'] = array('IN',$ids);
            $parameter['name'] = $name;
        }
        /**从商家列表跳转过来*/
        if(!empty($_GET['shop_id'])){
            $w['shop_id'] = $_GET['shop_id'];
            $parameter['shop_id'] = $_GET['shop_id'];
            $this->assign('shop_id',$_GET['shop_id']);
        }
        $list = $this->picture->selectPicture($w,'ctime DESC',15,$parameter);
        $data = $list['list'];
        if(!empty($data)){
            foreach($data as $k=>$v){
                $data[$k]['shop_name'] = M('Shop')->where(array('shop_id'=>$v['shop_id']))->getField('name');
            }
        }
        $this->assign('list',$data);

        $this->assign('page',$list['page']);
        $this->assign('request',$_REQUEST);
        $this->display('pictureList');
    }


    /**
     * 更改图片显示状态
     * @author mss
     * @time 2017-12-14
     */
    public function ajaxStatus(){
        $pic_id = $_POST['id'];
        $status = $_POST['status'];
        $w['pic_id'] = $pic_id;
        $data['status'] = $status;
        $data['utime'] = time();
        $res = $this->picture->editPicture($w,$data);
        if($res){
            $this->success('修改成功');
        }else{
            $this->error("修改失败");
        }
    }


    /**
     * 删除图片
     * @author mss
     * @time 2017-12-14
     */
    public function delPicture(){
        if(empty($_REQUEST['pic_id'])){
            $this->error('您未选择任何操作对象');
        }
        $where['pic_id'] = array('IN',I('request.pic_id'));
        $data['status'] = 9;
        $data['utime'] = time();
        $upd_res = $this->picture->editPicture($where,$data);
        if($upd_res){
            //其他删除操作

            $this->success('删除操作成功');
        }else{
            $this->error('删除操作失败');
        }
    }

}